package com.yueban.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yueban.R;
import com.yueban.model.BaseResponse;
import com.yueban.model.ResultCode;
import com.yueban.model.User;
import com.yueban.net.NetWorkController;
import com.yueban.util.SPManager;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class RegisterActivity extends AppCompatActivity {


    private EditText account;
    private EditText name;
    private EditText password;
    private EditText rePassword;
    private CheckBox checkBox;
    private TextView agreement;
    private Button register;

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        setSupportActionBar(toolbar);
        ;
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(R.string.action_register);
            toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        account = (EditText) findViewById(R.id.register_account);
        name = (EditText) findViewById(R.id.register_name);
        password = (EditText) findViewById(R.id.register_password);
        rePassword = (EditText) findViewById(R.id.register_re_password);
        checkBox = (CheckBox) findViewById(R.id.register_check);
        agreement = (TextView) findViewById(R.id.aboutAgreement);
        register = (Button) findViewById(R.id.register);
        progressBar = (ProgressBar) findViewById(R.id.register_progressBar);
        agreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RegisterActivity.this, AgreementActivity.class));
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String accountText = account.getText().toString().trim();
                String nameText = name.getText().toString().trim();
                String passwordText = password.getText().toString().trim();
                String rePasswordText = rePassword.getText().toString().trim();

                if ("".equals(nameText)) {
                    name.setError("请输入您的姓名");
                    return;
                }
                if ("".equals(accountText)) {
                    account.setError("请输入电子邮件地址或手机号");
                    return;
                }
                if ("".equals(passwordText)) {
                    password.setError("请输入密码");
                    return;
                }
                if ("".equals(rePasswordText)) {
                    rePassword.setError("请确认密码");
                    return;
                }
                if (!rePasswordText.equals(passwordText)) {
                    rePassword.setError("两次输入的密码不一致");
                    return;
                }
                if (!checkBox.isChecked()) {
                    Snackbar.make(view, "请同意用户协议", Snackbar.LENGTH_LONG)
                            .setAction("", null).show();
                    return;
                }
                attemptRegister(nameText, accountText, passwordText);
            }
        });

    }

    private void attemptRegister(String name, final String account, String password) {
        showProgressBar(true);
        NetWorkController netWorkController = new NetWorkController();
        netWorkController.register(name, account, password).subscribe(new Observer<BaseResponse<User>>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull BaseResponse<User> userBaseResponse) {
                if (userBaseResponse.getStatus() == ResultCode.SUCCESS) {
                    SPManager spManager = new SPManager(getApplication());
                    User user = userBaseResponse.getData();
                    if (user != null) {
                        spManager.setUserName(user.getName());
                        spManager.setUserId(user.getId());
                        spManager.setPassword(user.getPassword());
                    }
                    Snackbar.make(register, "注册成功", Snackbar.LENGTH_LONG)
                            .setAction("", null).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 500);
                } else {
                    showProgressBar(false);
                    Toast.makeText(RegisterActivity.this, userBaseResponse.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                showProgressBar(false);
                Toast.makeText(RegisterActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void showProgressBar(boolean show) {
        progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
        register.setVisibility(show ? View.GONE : View.VISIBLE);
    }

}
